/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.CodeBarre;
import Controller.Fournisseur;
import Controller.Marque;
import Controller.Materiel;
import Controller.Salle;
import Controller.Secteur;
import Model.FournisseurDAO;
import Model.MarqueDAO;
import Model.MaterielDAO;
import Model.SalleDAO;
import Model.SecteurDAO;
import java.sql.Connection;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Romain
 */
public class jframeMateriel extends javax.swing.JFrame {
    private Connection connect;
    private Controller.Type leType;
    

  public jframeMateriel(Connection connect, Controller.Type type) {
	initComponents();
        this.connect = connect;
        this.initCBO();
        this.leType = type;
		
  }
  
  public Materiel CreerMateriel()
  {
      try {
            Materiel mat = new Materiel();
            mat.setNumCompta(0);
            mat.setNumSerie(this.tfNumeroDeSerie.getText());
            mat.setNom(this.tfNom.getText());
            mat.setDescriptif(this.tfDescriptif.getText());
            
            Calendar gc = Calendar.getInstance();
            gc.set(Integer.valueOf(this.cboDateAchatAnnee.getSelectedItem().toString()),Integer.valueOf(this.cboDateAchatMois.getSelectedItem().toString()) - 1,Integer.valueOf(this.cboDateAchatJour.getSelectedItem().toString()));
            mat.setDateHT(gc.getTime());
            
            mat.setPrixHT(Double.valueOf(this.tfPrix.getText()));
            mat.setGarantie(Integer.valueOf(this.tfGarantie.getText()));
            mat.setNumBC(this.tfBonDeCommande.getText());
            mat.setBudget("");
            mat.setAmortissement(Integer.valueOf(this.tfAmortissement.getText()));
            mat.setType(this.leType);
            mat.setFournisseur((Fournisseur) this.cboFournisseur.getSelectedItem());
            mat.setMarque((Marque) this.cboMarque.getSelectedItem());
            mat.setSalle((Salle) this.cboSalle.getSelectedItem());
            mat.setSecteur((Secteur) this.cboSecteur.getSelectedItem());
            
            CodeBarre cb = new CodeBarre(mat, connect);
            mat.setNumCB(cb.creerCodeBarre());
            
            return mat;
     
        }catch(Exception e)
        {
            System.out.println("bug");
            return null;
        }
      
  }
  
  public void initCBO()
  {
   
        FournisseurDAO fournisseurDAO = new FournisseurDAO(connect);
        MarqueDAO marqueDAO = new MarqueDAO(connect);
        SalleDAO salleDAO = new SalleDAO(connect);
        SecteurDAO secteurDAO = new SecteurDAO(connect);

        Map<Integer, Fournisseur> fournisseurs = new HashMap();

        fournisseurs = fournisseurDAO.findAll();
        for (Integer mapKey : fournisseurs.keySet()) {
          // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
          cboFournisseur.addItem(fournisseurs.get(mapKey));
        }

        Map<Integer,Marque> marques = new HashMap();
        marques = marqueDAO.findAll();
        for (Integer mapKey : marques.keySet()) {
          // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
          cboMarque.addItem(marques.get(mapKey));
        }

        Map<Integer,Salle> salles = new HashMap();
        salles = salleDAO.findAll();
        for (Integer mapKey : salles.keySet()) {
          // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
          cboSalle.addItem(salles.get(mapKey));
        }

        Map<Integer,Secteur> secteurs = new HashMap();
        secteurs = secteurDAO.findAll();
        for (Integer mapKey : secteurs.keySet()) {
          // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
          cboSecteur.addItem(secteurs.get(mapKey));
        }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfNumeroDeSerie = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfNom = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfDescriptif = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfPrix = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfGarantie = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tfBonDeCommande = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        tfAmortissement = new javax.swing.JTextField();
        cboFournisseur = new javax.swing.JComboBox();
        cboMarque = new javax.swing.JComboBox();
        cboSalle = new javax.swing.JComboBox();
        cboSecteur = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        bEnregistrer = new javax.swing.JButton();
        bAnnuler = new javax.swing.JButton();
        cboDateAchatJour = new javax.swing.JComboBox();
        cboDateAchatMois = new javax.swing.JComboBox();
        cboDateAchatAnnee = new javax.swing.JComboBox();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabel1.setText("Materiel Generique");

        jLabel2.setText("Numero de Serie :");

        jLabel3.setText("Nom :");

        jLabel4.setText("Descriptif :");

        jLabel5.setText("Date d'achat :");

        jLabel6.setText("Prix ( HT ) :");

        jLabel7.setText("Duree Garantie ( M ) : ");

        jLabel8.setText("Bon de commande : ");

        jLabel9.setText("Amorti le :");

        tfAmortissement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfAmortissementActionPerformed(evt);
            }
        });

        cboSalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboSalleActionPerformed(evt);
            }
        });

        jLabel10.setText("Fournisseur");

        jLabel11.setText("Marque");

        jLabel12.setText("Salle");

        jLabel13.setText("Secteur");

        bEnregistrer.setText("Enregistrer");
        bEnregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEnregistrerActionPerformed(evt);
            }
        });

        bAnnuler.setText("Annuler");
        bAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAnnulerActionPerformed(evt);
            }
        });

        cboDateAchatJour.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        cboDateAchatMois.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));

        cboDateAchatAnnee.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfBonDeCommande, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bEnregistrer)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(158, 158, 158)
                                .addComponent(bAnnuler))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cboFournisseur, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboMarque, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboSalle, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel12))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addComponent(cboSecteur, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(72, 72, 72))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(48, 48, 48)))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(32, 32, 32)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel2))
                                .addGap(14, 14, 14)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(tfNom, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                        .addComponent(tfPrix)
                                        .addComponent(tfNumeroDeSerie))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cboDateAchatJour, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cboDateAchatMois, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cboDateAchatAnnee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfGarantie, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfAmortissement, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(tfDescriptif, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfNumeroDeSerie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfDescriptif, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(tfGarantie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboDateAchatJour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboDateAchatMois, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboDateAchatAnnee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tfPrix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(tfAmortissement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(tfBonDeCommande, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboFournisseur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboMarque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboSalle)
                    .addComponent(cboSecteur))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bEnregistrer)
                    .addComponent(bAnnuler))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfAmortissementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfAmortissementActionPerformed
	  // TODO add your handling code here:
    }//GEN-LAST:event_tfAmortissementActionPerformed

  private void bAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAnnulerActionPerformed
	// TODO add your handling code here:
	this.setVisible(false);
  }//GEN-LAST:event_bAnnulerActionPerformed

    private void cboFournisseurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboFournisseurActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboFournisseurActionPerformed

    private void bEnregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEnregistrerActionPerformed
        new MaterielDAO(connect).insert(this.CreerMateriel());
    }//GEN-LAST:event_bEnregistrerActionPerformed

    private void cboSalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboSalleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboSalleActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAnnuler;
    private javax.swing.JButton bEnregistrer;
    private javax.swing.JComboBox cboDateAchatAnnee;
    private javax.swing.JComboBox cboDateAchatJour;
    private javax.swing.JComboBox cboDateAchatMois;
    private javax.swing.JComboBox cboFournisseur;
    private javax.swing.JComboBox cboMarque;
    private javax.swing.JComboBox cboSalle;
    private javax.swing.JComboBox cboSecteur;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField tfAmortissement;
    private javax.swing.JTextField tfBonDeCommande;
    private javax.swing.JTextField tfDescriptif;
    private javax.swing.JTextField tfGarantie;
    private javax.swing.JTextField tfNom;
    private javax.swing.JTextField tfNumeroDeSerie;
    private javax.swing.JTextField tfPrix;
    // End of variables declaration//GEN-END:variables
}
